/**
 * @author 0283000115
 * @date 2017年9月4日-下午5:11:24
 */
package com.zte.claa.inficombo.csa.app.dao.mysql.jdbcdao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * @author 0283000115
 *
 */
public class BaseDao {
	// 日志管理器
	protected Logger logger = LoggerFactory.getLogger(BaseDao.class);
	
	// 数据源.
	protected DruidDataSource dataSource;
	
	/**
	 * 构建器
	 * 
	 * @param dataSource
	 */
	public BaseDao(DruidDataSource dataSource){
		this.dataSource = dataSource;
	}
	
	/**
	 * 从连接池获取数据库连接
	 * 
	 * @return
	 * @throws Exception
	 */
	public Connection getDBConnection() throws Exception {
		return this.dataSource.getConnection();
	}
	
	/**
	 * 根据项目id获取对应的库名。
	 * 
	 * @param projectId
	 * @return
	 */
	public String getProjDBName(String projectId){
		return "db_"+projectId.toLowerCase().trim();
	}
	
	/**
	 * 释放查询结果集
	 * 
	 * @param ret
	 */
	public void releaseResultset(ResultSet ret){
		if(ret != null){
			try {
				ret.close();
			} catch (SQLException e) {
				logger.error("Release resultset failed: "+e.getMessage());
			}
		}
	}
	
	/**
	 * 释放sql statement
	 * 
	 * @param st
	 */
	public void releaseStatement(Statement st){
		if(st != null){
			try {
				st.close();
			} catch (SQLException e) {
				logger.error("Release statement failed: "+e.getMessage());
			}
		}
	}
	
	/**
	 * 释放数据库连接
	 * 
	 * @param conn
	 */
	public void releaseConnection(Connection conn){
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("Release connection failed: "+e.getMessage());
			}
		}
	}
	
}
